/*
 jquery form提交数据
 url:提交的action
 successUrl:操作成功后的action
 successMessage:操作成功后的提示消息
 */
function ajaxFormSubmit(url, successUrl, successMessage){
    if (successMessage == undefined) {
        successMessage = "操作成功!";
    }
    
    var options = {
        url: url,
        type: 'post',
        dataType: 'json',
        success: function(data){
            if (data.success) {
                jSuccess(successMessage, null, function(r){
                    top.main.location = successUrl;
                });
            }
            else {
                jError(data.errorMsg);
            }
        }
    };
    
    $("form").ajaxSubmit(options);
    
}
function ajaxFormSubmitOuter(url, successUrl, successMessage){
    if (successMessage == undefined) {
        successMessage = "操作成功!";
    }
    
    var options = {
        url: url,
        type: 'post',
        dataType: 'json',
        success: function(data){
            if (data.success) {
                jSuccess(successMessage, null, function(r){
                    window.location.href = successUrl;
                });
            }
            else {
                jError(data.errorMsg);
            }
        }
    };
    $("#formObj").ajaxSubmit(options);
    //$("form").ajaxSubmit(options);
}

/*
 jquery form提交数据
 url:提交的action
 successUrl:操作成功后的action
 successMessage:操作成功后的提示消息
 */
function ajaxFormSubmit4Print(url, successMessage){
    if (successMessage == undefined) {
        successMessage = "操作成功!";
    }
    
    var options = {
        url: url,
        type: 'post',
        dataType: 'json',
        success: function(data){
            if (data.success) {
                jSuccess(successMessage, null, null);
            }
            else {
                jError(data.errorMsg);
            }
        }
    };
    
    $("form").ajaxSubmit(options);
}

/*
 jquery url提交数据
 url:提交的action
 successMessage:操作成功后的提示消息
 */
function ajaxUrlSubmit(url, successMessage){
    if (successMessage == undefined) {
        successMessage = "操作成功!";
    }
    $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        success: function(data){
            if (data.success) {
                jSuccess(successMessage, null, function(r){								
                    top.main.location.reload();
                });
            }
            else {
                jError(data.errorMsg);
            }
        },
        error: function(data){
        }
    });
}
function ajaxFormSubmit2(url, successMessage){
    if (successMessage == undefined) {
        successMessage = "操作成功!";
    }
    $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        success: function(data){
            if (data.success) {
                jSuccess(successMessage, null, function(r){								
                    top.main.location.reload();
                });
            }
            else {
                jError(data.errorMsg);
            }
        },
        error: function(data){
        }
    });
}
/*
 选择一个checkbox返回id
 */
function selectOne(checkName){
    var checkIds = document.getElementsByName(checkName);
    var cNum = 0;
    var tempId = false;
    for (var i = 0; i < checkIds.length; i++) {
        if (checkIds[i].checked == true) {
            cNum++;
            tempId = checkIds[i].value;
        }
    }
    if (cNum == 0) {
        jAlert("未选择任何一项");
        return false;
    }
    if (cNum > 1) {
        jAlert("只能选择一项");
        return false;
    }
    return tempId;
}

/*
 选择多个checkbox返回id,id,id
 */
function selectSome(checkName){
    var checkIds = document.getElementsByName(checkName);
    var cNum = 0;
    var tempId = "";
    for (var i = 0; i < checkIds.length; i++) {
        if (checkIds[i].checked == true) {
            cNum++;
            tempId += checkIds[i].value + ",";
        }
    }
    if (cNum == 0) {
        jAlert("未选择任何一项");
        return false;
    }
    return tempId.substring(0, tempId.length - 1);
}

/*
 选择所有的checkbox
 */
function selectAll(allObj, checkName){
    var checkIds = document.getElementsByName(checkName);
    for (var i = 0; i < checkIds.length; i++) {
        if (allObj.checked == true) {
			if(!checkIds[i].disabled)
              checkIds[i].checked = true;
        }
        else {
            checkIds[i].checked = false;
        }
    }
}

/*
 选择一个checkbox返回id
 */
function selectOneByIfm(checkName, iframeId){
    var checkIds = iframeId.document.getElementsByName(checkName);
    var cNum = 0;
    var tempId = false;
    for (var i = 0; i < checkIds.length; i++) {
        if (checkIds[i].checked == true) {
            cNum++;
            tempId = checkIds[i].value;
        }
    }
    if (cNum == 0) {
        jAlert("未选择任何一项");
        return false;
    }
    if (cNum > 1) {
        jAlert("只能选择一项");
        return false;
    }
    return tempId;
}

/*
 选择多个checkbox返回id,id,id
 */
function selectSomeByIfm(checkName, iframeId){
    var checkIds = iframeId.document.getElementsByName(checkName);
    var cNum = 0;
    var tempId = "";
    for (var i = 0; i < checkIds.length; i++) {
        if (checkIds[i].checked == true) {
            cNum++;
            tempId += checkIds[i].value + ",";
        }
    }
    if (cNum == 0) {
        jAlert("未选择任何一项");
        return false;
    }
    return tempId.substring(0, tempId.length - 1);
}

/*
 选择所有的checkbox
 */
function selectAllByIfm(allObj, checkName, iframeId){
    var checkIds = iframeId.document.getElementsByName(checkName);
    for (var i = 0; i < checkIds.length; i++) {
        if (allObj.checked == true) {
            checkIds[i].checked = true;
        }
        else {
            checkIds[i].checked = false;
        }
    }
}

/*
 判断是否是数字
 */
function IsDigit(){
    return ((event.keyCode >= 48) && (event.keyCode <= 57));
}

/*
 【对话框】组织机构选择（毕业生就业主管部门）
 */
function orgSelDialog(){
    var url = "/bys/system/common/orgSel.as?id=0&kind=dept";
    window.showModalDialog(url, document, "dialogWidth=600px;dialogHeight=400px");
}

/*
 【对话框】组织机用户构选择（毕业生就业主管部门）
 */
function orgUserSelDialog(){
    var url = "/bys/system/common/orgUserSel.as?id=0";
    window.showModalDialog(url, document, "dialogWidth=600px;dialogHeight=400px");
}

/*
 【对话框】专业代码选择
 */
function majorSelDialog(){
    var url = "/bys/system/common/majorSel.as";
    window.showModalDialog(url, document, "dialogWidth=600px;dialogHeight=400px");
}

/*
 【对话框】第二专业代码选择
 */
function secondMajorSelDialog(){
    var url = "/bys/system/common/majorSel.as?kind=second";
    window.showModalDialog(url, document, "dialogWidth=600px;dialogHeight=400px");
}

/*
 【对话框】区域代码选择
 */
function areaSelDialog(columns){
    var url = "/bys/system/common/areaSel.as?columns="+columns;
    window.showModalDialog(url, document, "dialogWidth=600px;dialogHeight=400px");
}


/*
 【对话框】院校代码选择
 */
function schoolSelDialog(){
    var url = "/bys/system/common/schoolSel.as";
    window.showModalDialog(url, document, "dialogWidth=600px;dialogHeight=400px");
}

/*
 【对话框】指定院校的院校专业树形选择
 */
function schoolTreeSelDialog(){
    var school = $("#school").attr("value");
    var url = "/bys/system/common/schoolTreeSel.as?id=0&school=" + school;
    window.showModalDialog(url, document, "dialogWidth=600px;dialogHeight=400px");
}

/*
 【对话框】组织机构角色选择
 */
function roleSelDialog(orgId){
    var url = "/bys/system/role/dialog.as?orgId=" + orgId;
    window.showModalDialog(url, document, "dialogWidth=600px;dialogHeight=400px");
}

/*
 【树形菜单】组织机构
 */
function orgTree(kind){
    var url = "/bys/system/common/orgTree.as?kind=" + kind + "&id=0";
    top.treeFrame.location = url;
}

/*
 【树形菜单】省份与直辖市
 */
function provinceTree(){
    var url = "/bys/system/common/provinceTree.as?province=0";
    top.treeFrame.location = url;
}

/*
 【树形菜单】院校
 */
function schoolTree(){
    var url = "/bys/system/common/schoolTree.as";
    top.treeFrame.location = url;
}

/*
 【导入】
 */
function importExcel(moduleName, channelName){
    if (moduleName == undefined) {
        alert("模块名称[moduleName=]" + moduleName + "不能为空");
        return;
    }
    if (channelName == undefined) {
        alert("栏目名称[channelName=]" + channelName + "不能为空");
        return;
    }
    var url = "/bys/exchange/preImport.as?moduleName=" + moduleName + "&channelName=" + channelName;
    jwin(url, 450, 220);
    //showTB(url,450,220,"iframe");
}

/*
 【导出】
 */
function exportExcel(command){
    if (command.indexOf("general") == -1 && command.indexOf("advanced") == -1 && command.indexOf("noQuery") == -1) {
        alert("command需为【general或者advanced或者noQuery】,而不是【" + command + "】");
		return;
    }
    var moduleName;
    var channelName;
    var showTBUrl = "/bys/exchange/export.as?command=" + command;
    $("form input").each(function(){
        var name = $(this).attr("name");
        var value = $(this).attr("value");
        if (name.indexOf("searchId") != -1) {
            if (!value) {
                alert("searchId不能为空");
				return;
            }
            showTBUrl = showTBUrl + "&" + name + "=" + value;
        }
        if (name.indexOf("moduleName") != -1) {
            if (!value) {
                alert("moduleName不能为空");
				return;
            }
            showTBUrl = showTBUrl + "&" + name + "=" + value;
        }
        if (name.indexOf("channelName") != -1) {
            if (!value) {
                alert("channelName不能为空");
				return;
            }
            showTBUrl = showTBUrl + "&" + name + "=" + value;
			var length = $("form input").find("#channelName").length;
			if(length > 1){
				for(var i = 1 ;i < length; i ++){
    				$("form input").remove($("form input").find("#channelName")[i]);
				}
			}
        }
    });
	var queryUrl = "/bys/exchange/preExport.as?command=" + command;
    if (command.indexOf("noQuery") == -1) {
        var options = {
            url: queryUrl,
            type: 'post',
            dataType: 'json',
            success: function(data){
                if (data.success) {
                    jwin(showTBUrl, 450, 220, "数据导出");
                }
                else {
                    jwin(showTBUrl, 450, 220, "数据导出");
                }
            }
        };
        $("form").ajaxSubmit(options);
    }else{
		 jwin(noQueryUrl, 450, 220, "数据导出");
	}
}

/*
 【thickbox窗口】
 */
function showTB(url, width, height, invokeWay){
    if (width == undefined) {
        width = 600;
    }
    if (height == undefined) {
        height = 300;
    }
    var iw = "modal=true";
    if (invokeWay == "iframe") {
        iw = "TB_iframe=true";
    }
    if (url.indexOf("?") != -1) {
        url += "&width=" + width + "&height=" + height + "&" + iw;
    }
    else {
        url += "?width=" + width + "&height=" + height + "&" + iw;
    }
    tb_show(url);
}

/*
 【jwin窗口】
 */
function jwin(url, width, height,title,posx,posy){
    if (width == undefined) {
        width = 600;
    }
    if (height == undefined) {
        height = 350;
    }
	if (posx == undefined) {
        posx = 100;
    }
	 if (posy == undefined) {
        posy = 15;
    }
    if (title == undefined) {
        title = "互动中心";
    }
    $().newWindow({
        content: "<iframe width='" + width + "px' height='" + height + "px' src='" + url + "' frameborder='0'>",
        windowType: "iframe",
        width: width,
        height: height,
        windowTitle: title,
		posx: posx,
        posy: posy
    });
}
/*
  【关闭jwin窗口】
*/
function closeJwin() {
  var node = parent.document.getElementById("jwin-id");
  parent.document.body.removeChild(node);
}
/*
  【jwin-login窗口】
*/
function jLogin(){
	var url="/bys/common/page/login.jsp";
	var width=350;
	var height=150;
	var title="毕业生就业公共网";
	$().newWindow({
			 content: "<iframe width='"+width+"px' height='"+height+"px' src='"+url+"' frameborder='0'>",
			 windowType: "iframe",
			 width:width,height:height,
			 windowTitle : title
	});
}

/*
 【高级查找】预检索
 */
function preAdvancedSearch(searchId){
    var url = "/bys/common/search/page.as?type=page&searchId=" + searchId;
    top.mainFrame.location = url;
}

/*
 【高级查找】检索
 */
function advancedSearch(){
    $("form").attr("action", "/bys/common/search/list.as?type=list");
    $("form").submit();
}

/*
 【高级查找】重新检索
 */
function reAdvancedSearch(){
    var searchId = $("#searchId").attr("value");
    var url = "/bys/common/search/page.as?type=page&searchId=" + searchId;
    top.mainFrame.location = url;
}

/*
 【新增操作时：附件编辑】
 */
function attachEditForAdd(moduleName){
    var url = "/bys/file/preAddFile.as?moduleName=" + moduleName;
    jwin(url, 450, 250,"编辑附件");
}

/*
 【更新操作时：附件编辑】
 */
function attachEditForUpdate(moduleName, foreignId){
    var url = "/bys/file/preUpdateFile.as?moduleName=" + moduleName + "&foreignId=" + foreignId;
    jwin(url, 450, 250,"编辑附件");
}

/*
 【隐藏中间树菜单】
 */
function hideMiddleTree(){
    if (top.document.getElementById("contentFrameset") != null) 
        top.document.getElementById("contentFrameset").cols = "237,0,*";
}

/*
 【初始化中间树菜单】
 */
function initMiddleTree(){
    top.document.getElementById("contentFrameset").cols = "237,210,*";
    top.mainFrame.location = "/bys/common/page/main.jsp";
}

/*
 【审核意见处理】
 */
function actorDesc(url, batch){
	var ids="";
    if (batch == "batch" || batch == true) {//批量处理
        ids = selectSome("idx");
    }else
		ids = selectOne("idx");
	if (!ids) {
            return;
    }
	if(url.indexOf("?")!=-1)url += "&ids=" + ids;
	else url += "?ids=" + ids;
    url = url.replaceAll("\\?", "-");
    url = url.replaceAll("\\&", "!");
    var url2 = "/bys/common/actorDesc.as?url=" + url;
    jwin(url2, 450, 192,"填写意见");
}

/*
 【审核意见处理】
 */
function actorCheckDesc(urlPass,urlUnPass, batch){
    if (batch == "batch" || batch == true) {//批量处理
        var ids = selectSome("idx");
        if (!ids) {
            return;
        }
		if(urlPass.indexOf("?")!=-1)urlPass += "&ids=" + ids;
		else urlPass += "?ids=" + ids;
		
		if(urlUnPass.indexOf("?")!=-1)urlUnPass += "&ids=" + ids;
		else urlUnPass += "?ids=" + ids;
    }else {
        var id = selectOne("idx");
        if (!id) {
            return;
        }
		if(urlPass.indexOf("?")!=-1)urlPass += "&id=" + id;
		else urlPass += "?id=" + id;
		
		if(urlUnPass.indexOf("?")!=-1)urlUnPass += "&id=" + id;
		else urlUnPass += "?id=" + id;
    }
    urlPass = urlPass.replaceAll("\\?", "-");
    urlPass = urlPass.replaceAll("\\&", "!");
	
	urlUnPass = urlUnPass.replaceAll("\\?", "-");
    urlUnPass = urlUnPass.replaceAll("\\&", "!");
	
    var url2 = "/bys/common/actorDesc.as?urlPass=" + urlPass+"&urlUnPass="+urlUnPass;
    jwin(url2, 450, 192,"填写审核意见");
}

/*
【通用调用】
_iv:接口名@方法名
batch:是否批量处理
channelName:栏目名称
*/
function iv(_in,batch,channelName){
	var ids="";
	var url="/bys/common/json.as?_in="+_in;
	if(batch=="true" || batch==true){
		ids=selectSome("idx");
	}else {
		ids=selectOne("idx");
	}
	if(!ids){
		return;
	}
	url+="&ids="+ids;
	if(channelName != undefined && channelName!=""){
		url+="&channelName="+channelName;
	}
	jConfirm("确认要执行操作吗？",null,function(r) {
		 if(r) {
			ajaxUrlSubmit(url,"操作成功！");
		 }
	});
}

String.prototype.replaceAll = function(s1, s2){
    return this.replace(new RegExp(s1, "gm"), s2);
}
/*----------jSearch start------------*/
//obj:事件源
function jSearch(obj,sql){
	var idObj=obj.id
	if(idObj==undefined || idObj==""){
		alert('该html元素要有id属性');
		return;
	}
   var url=encodeURI('/bys/jSearch?sql='+sql);
   $.ajax({
        type: "POST",
        url: url,
        dataType: "html",
        success: function(data){
		   var jSearchDiv = document.getElementById('jSearch')
		   if(jSearchDiv=="" || jSearchDiv==undefined){
			   alert("要有一个id为jSearch的div");
			   return;
		   }
		   var strs = data.split("\n");
		   jSearchDiv.innerHTML="";
           for(i=0; i < strs.length - 1; i++) {
				var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
				suggest += 'onmouseout="javascript:suggestOut(this);" ';
				suggest += 'onclick="javascript:setSearch(this.innerHTML,'+idObj+');" ';
				suggest += 'class="s_link">' + strs[i] + '</div>';
				jSearchDiv.innerHTML += suggest;
			}
        }
    });
}

function suggestOver(divObj) {
	divObj.className = 's_link_over';
}

function suggestOut(divObj) {
	divObj.className = 's_link';
}

function setSearch(value,idObj) {
	document.getElementById(idObj.id).value=value;
	document.getElementById('jSearch').innerHTML ="";
}
/*----------jSearch end------------*/
/*----------other menu start------------*/
var timeout         = 100;
var closetimer		= 0;
var ddmenuitem      = 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose; 
/*----------other menu end------------*/

/* 设置功能按钮位置 */
function setBtnPos() {  
  var menuWidth = 0;
  var btnCount = 0;
  $("input").each(function() {
    var type = $(this).attr("type");
	var name = $(this).attr("name");
	if(type == "button" && name == "btnmenu") {
	  menuWidth += $(this)[0].clientWidth;
	  btnCount++;
	}
  });
  
  var padding = (btnCount + 2) * 10;
  $("#menuTd").css('width',menuWidth + padding);
}



/*
 【打印调用通用接口】
 */
 function printTable(){
    var url="/bys/system/common/print.as";
    var obj = window.dialogArguments;
    obj.open(url,"","")
    //window.showModalDialog(url,document,"dialogWidth=1px;dialogHeight=1px;dialogLeft=-10px;dialogTop=-10px");//打开打印窗口
 }
 
 /*
 【打印】使用选择多项
 */
function selectPrintSome(checkName){
    var checkIds = document.getElementsByName(checkName);
    var cNum = 0;
    var tempId = "";
    for (var i = 0; i < checkIds.length; i++) {
        if (checkIds[i].checked == true) {
            cNum++;
            tempId += checkIds[i].value + ",";
        }
    }
    if (cNum == 0) {
       return "";
    }
    return tempId.substring(0, tempId.length - 1);
}

/*
 【福建省设区市】选择对话框
 */
function areaFJSel(){
    var url = "/bys/system/common/areaFJ.as";
    jwin(url, 350, 150, "福建省设区市");
}

/*【翻页开始】*/
function firstPage(){
	$("#pageNo").val(1);
	$("#initPageNo").val(false);
	$("form").submit();
}

function lastPage(){
	var pageNo = Number($("#totalPageID").html());
	$("#pageNo").val(pageNo);
	$("#initPageNo").val(false);
	$("form").submit();
}

function nextPage(){
	var pageNo = Number($("#pageNo").attr("value"));
	var totalPage = Number($("#totalPageID").html());
	
	if(pageNo>=totalPage){
		$("#pageNo").val(totalPage);
	}else{
		$("#pageNo").val(pageNo+1);
	}
	$("#initPageNo").val(false);
	$("form").submit();
}

function prePage(){
	var pageNo = Number($("#pageNo").attr("value"));
	
	if(pageNo<=1){
		pageNo=1;
	}else{
		$("#pageNo").val(pageNo-1);
	}
	$("#initPageNo").val(false);
	$("form").submit();
}

function goPage(){
	var pageNo = Number($("#pageNo").attr("value"));
	var totalPage = Number($("#totalPageID").html());
	
	if(pageNo>=totalPage){
		$("#pageNo").val(totalPage);
	}else if(pageNo<=1){
		$("#pageNo").val(1);
	}
	$("#initPageNo").val(false);
	$("form").submit();
}

function changePageSize(obj){
	$("#initPageNo").val(false);
	$("form").submit();
}
/*【翻页结束】*/